package com.nf.health.app.adapter;

import android.app.Activity;
import android.content.Intent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;

import com.nf.health.app.R;
import com.nf.health.app.customview.SwipeView;
import com.nf.health.app.db.DbManager;
import com.nf.health.app.inquiry.bean.PatientUser;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

/**
 * Created by Jason on 2016/3/17.
 */
public class PatientUserAdapter extends BaseAdapter {
    private final static int TYPE_ADD = 0;
    private final static int TYPE_USER = 1;
    List<PatientUser> users;
    private Activity activity;
    public ArrayList<SwipeView> unClosedSwipeView = new ArrayList<SwipeView>();

    public PatientUserAdapter(Activity activity, List<PatientUser> users) {
        this.activity = activity;
        this.users = users;
    }
    @Override
    public int getCount() {
        return users == null ? 1 : users.size()+1;
    }

    @Override
    public Object getItem(int position) {
        return users.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public int getItemViewType(int position) {
        if(position == 0){
            return TYPE_ADD;
        }
        return TYPE_USER;
    }

    @Override
    public int getViewTypeCount() {
        return 2;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if(getItemViewType(position) == TYPE_ADD){
            holder = ViewHolder.get(parent, convertView, R.layout.layout_item_add_user, position);
            holder.setText(R.id.tv_inquiry_key, "添加问诊人");
        }else{
            holder = ViewHolder.get(parent, convertView, R.layout.item_patient_user, position);
            PatientUser item = users.get(position - 1);
            holder.setText(R.id.tv_patient_name, item.getNick());
            int gender = item.getGender();
            String genderStr = gender == 1? "男":"女";


            try{
                int year = Integer.parseInt(item.getBirthday().split("-")[0]);
                int age = Calendar.getInstance().get(Calendar.YEAR) - year;
                holder.setText(R.id.tv_patient_info, "("+genderStr+","+age+"岁)");
            }catch (Exception e){
                e.printStackTrace();
            }

            SwipeView swipeView  = holder.getView(R.id.swipeView);
            View ll_content  = holder.getView(R.id.ll_content);
            View delete  = holder.getView(R.id.delete);
            delete.setTag(position - 1);
            delete.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {
                    int position = (Integer) v.getTag();
                    int num = DbManager.getInstance().deletePatient(users.get(position).getId());
                    if(num == 1){
                        unClosedSwipeView.clear();
                        users.remove(position);
                        notifyDataSetChanged();
                    }
                }
            });
            ll_content.setTag(position - 1);
            ll_content.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {
                    if (unClosedSwipeView.size() > 0) {
                        closeAllOpenedSwipeView();
                    } else {
                        int position = (Integer) v.getTag();
                        //TODO
                        Intent intent = new Intent();
                        intent.putExtra("patient", users.get(position));

                        activity.setResult(Activity.RESULT_OK, intent);
                        activity.finish();
                    }
                }
            });
            swipeView.setOnSwipeStatusChangeListener(new SwipeView.OnSwipeStatusChangeListener() {

                @Override
                public void onSwiping(SwipeView swipingSwipeView) {
                    if (!unClosedSwipeView.contains(swipingSwipeView)) {
                        closeAllOpenedSwipeView();
                    }
                    if (!unClosedSwipeView.contains(swipingSwipeView)) {
                        unClosedSwipeView.add(swipingSwipeView);
                    }
                }

                @Override
                public void onOpen(SwipeView openedSwipeView) {
                    for (int i = 0; i < unClosedSwipeView.size(); i++) {
                        if (unClosedSwipeView.get(i) != openedSwipeView) {
                            unClosedSwipeView.get(i).close();
                        }
                    }
                    if (!unClosedSwipeView.contains(openedSwipeView)) {
                        unClosedSwipeView.add(openedSwipeView);
                    }
                }

                @Override
                public void onClose(SwipeView closedSwipeView) {
                    unClosedSwipeView.remove(closedSwipeView);
                }
            });
        }
        return holder.getConvertView();
    }

    public void closeAllOpenedSwipeView() {
        for (int i = 0; i < unClosedSwipeView.size(); i++) {
            if (unClosedSwipeView.get(i).getSwipeStatus() != SwipeView.SwipeStatus.Close) {
                unClosedSwipeView.get(i).close();
            }
        }
    }
}
